package crud

import (
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ RolesModel = (*customRolesModel)(nil)

type (
	// RolesModel is an interface to be customized, add more methods here,
	// and implement the added methods in customRolesModel.
	RolesModel interface {
		rolesModel
		CountAll(ctx context.Context) (int64, error)
		FindAll(ctx context.Context, page int64, pageSize int64) ([]*Roles, error)
	}

	customRolesModel struct {
		*defaultRolesModel
	}
)

// CountAll counts all roles.
func (m *customRolesModel) CountAll(ctx context.Context) (int64, error) {
	query := fmt.Sprintf("select count(*) from %s where `deleted_at` IS NULL", m.table)
	var count int64
	err := m.QueryRowNoCacheCtx(ctx, &count, query)
	return count, err
}

// FindAll finds all roles with pagination.
func (m *customRolesModel) FindAll(ctx context.Context, page int64, pageSize int64) ([]*Roles, error) {
	offset := (page - 1) * pageSize
	query := fmt.Sprintf("select %s from %s where `deleted_at` IS NULL limit ? offset ?", rolesRows, m.table)
	var resp []*Roles
	err := m.QueryRowsNoCacheCtx(ctx, &resp, query, pageSize, offset)
	return resp, err
}

// NewRolesModel returns a model for the database table.
func NewRolesModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) RolesModel {
	return &customRolesModel{
		defaultRolesModel: newRolesModel(conn, c, opts...),
	}
}
